package com.sx.pipeline.ui.needs.construction.participation

import android.app.Application
import android.text.TextUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.req.AddInquiryDTO
import com.sx.pipeline.repos.http.req.AddQuotedPriceDTO
import com.sx.pipeline.repos.http.res.Ipage
import com.sx.pipeline.repos.http.res.ReleaseNeedsVO
import com.sx.pipeline.ui.task.entity.ReleaseTaskListVO

/**
 * @Author: YY
 * @Date: 2022/4/13
 * @Description:
 */
class ParticipationNeedsVM(app: Application): CYBaseViewModel(app) {
    /**
     * 获取我参与的需求列表
     */
    var mPage = 1
    val releaseSupplyListEvent: SingleLiveEvent<Ipage<ReleaseNeedsVO>> = SingleLiveEvent()
    fun takePartNeedPage(key:String,page: Int = 1) {
        addSubscribe(
            mModel.takePartNeedPage(key,page)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Ipage<ReleaseNeedsVO>, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Ipage<ReleaseNeedsVO>) {
                        releaseSupplyListEvent.postValue(result)
                        mUI.initRefreshEvent().postValue(true)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        mUI.initRefreshEvent().postValue(false)
                        if (page != 1) {
                            mUI.initLoadEvent().postValue(true)
                        }
                    }
                })
        )
    }


    /**
     * 需求详情
     */
    val needOrderDetailEvent: SingleLiveEvent<ReleaseNeedsVO> = SingleLiveEvent()
    fun getNeedOrderDetail(id: Int) {
        addSubscribe(
            mModel.getNeedOrderDetail(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<ReleaseNeedsVO, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: ReleaseNeedsVO) {
                        needOrderDetailEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }

    /**
     * 添加询价
     * @param orderId 订单id
     * @param contact 联系人
     * @param contactPhone 联系电话
     * @param quantity 供应数量
     * @param unit 计量单位
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    val auditSuccessEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    fun addQuotedPrice(
        orderId: Int,
        price: String,
        totalPrice: String,
        startTime: String,
        endTime: String,
        payWord:String
    ) {


        val dto = AddQuotedPriceDTO(orderId, price, totalPrice,startTime, endTime,payWord)
        addSubscribe(
            mModel.addQuotedPrice(dto)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Any, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Any) {
                        ToastUtils.showShort("添加报价成功")
                        finish()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }


    val needPriceEvent: SingleLiveEvent<Double> = SingleLiveEvent()
    fun getNeedQuote() {
        addSubscribe(
            mModel.getNeedQuote()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<Double, BaseViewModel<*>>
                    (this, true) {
                    override fun onResult(result: Double) {
                        needPriceEvent.postValue(result)
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                    }
                })
        )
    }
}